﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EncryptHelper;
using WebHelper.Handlers;
using WebHelper.Helpers;

namespace UtilitesWebApplication
{
    public partial class WebFormGenerateLicence : WebFormBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.DoPostPackInsteadAjax(RadButtonGenerate);
            }
        }

        protected void RadButtonGenerate_Click(object sender, EventArgs e)
        {
            byte[] key = new byte[] { 01, 76, 54, 12, 67, 34, 98, 65, 35, 88, 43, 67, 53, 26, 53, 85, 37, 62, 38, 52, 37, 65, 37, 42, 78, 106, 101, 102, 204, 209, 85, 83 };
            byte[] encryptedBytes = null;
            Dictionary<string, object> demoConfigrator = new Dictionary<string, object>();
            demoConfigrator.Add("LastVisited", null);
            demoConfigrator.Add("EndDate", null);
            demoConfigrator.Add("Unlimited", this.RadButtonUnlimited.Checked);
            if (this.RadButtonUnlimited.Checked)
            {
                demoConfigrator.Add("DefaultDuration", Convert.ToInt32(this.RadNumericTextBoxDuration.Text));
            }
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            using (MemoryStream mS = new MemoryStream())
            {
                formatter.Serialize(mS, demoConfigrator);
                encryptedBytes = DefaultEncryptHelper.Encrypt(mS.ToArray(), key);
            }
            File.WriteAllBytes(Server.MapPath("~/" + this.RadTextBoxFileName.Text), encryptedBytes);
            DownloadHandler.Download(new Uri( UrlHelper.GetCompleteUri("~/" + this.RadTextBoxFileName.Text)), this.RadTextBoxFileName.Text, true);
        }

        protected void RadButtonUnlimited_CheckedChanged(object sender, EventArgs e)
        {
            this.RadNumericTextBoxDuration.Visible = !this.RadButtonUnlimited.Checked;
        }


        protected void RadButtonRead_Click(object sender, EventArgs e)
        {
            byte[] key = new byte[] { 01, 76, 54, 12, 67, 34, 98, 65, 35, 88, 43, 67, 53, 26, 53, 85, 37, 62, 38, 52, 37, 65, 37, 42, 78, 106, 101, 102, 204, 209, 85, 83 };
            if (this.RadAsyncUpload.UploadedFiles.Count > 0)
            {
                byte[] encryptedBytes = new byte[this.RadAsyncUpload.UploadedFiles[0].ContentLength];
                this.RadAsyncUpload.UploadedFiles[0].InputStream.Read(encryptedBytes, 0, encryptedBytes.Length);
                byte[] decryptedBytes = DefaultEncryptHelper.Decrypt(encryptedBytes, key);
                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                using (MemoryStream mS = new MemoryStream(decryptedBytes))
                {
                    Dictionary<string, object> demoConfigrator = (Dictionary<string, object>)formatter.Deserialize(mS);
                    this.RadTextBoxReading.Text = "ID: " + demoConfigrator["Id"] + "\r\n"
                        + "Unlimited: " + demoConfigrator["Unlimited"] + "\r\n"
                        + "Duration: " + demoConfigrator["Duration"] + "\r\n";

                }
            }
            else
            {

            }
        }

    }
}